#!/bin/bash

# MongoDB 同步运行脚本
# 使用方法: ./run-sync.sh [full|incremental|realtime|verify|clean]

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查 Docker 和 Docker Compose
check_dependencies() {
    print_info "Checking dependencies..."
    
    if ! command -v docker &> /dev/null; then
        print_error "Docker is not installed"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        print_error "Docker Compose is not installed"
        exit 1
    fi
    
    print_success "Dependencies check passed"
}

# 启动数据库服务
start_databases() {
    print_info "Starting MongoDB databases..."
    
    docker-compose up -d mongodb-source mongodb-target
    
    print_info "Waiting for databases to be ready..."
    sleep 30
    
    # 检查数据库状态
    if docker-compose ps | grep -q "Up"; then
        print_success "Databases started successfully"
    else
        print_error "Failed to start databases"
        exit 1
    fi
}

# 全量同步
full_sync() {
    print_info "Starting full sync..."
    docker-compose --profile sync run --rm mongosync /scripts/full-sync.sh
    print_success "Full sync completed"
}

# 增量同步
incremental_sync() {
    print_info "Starting incremental sync..."
    docker-compose --profile sync run --rm mongosync /scripts/incremental-sync.sh
    print_success "Incremental sync completed"
}

# 实时同步
realtime_sync() {
    print_info "Starting real-time sync..."
    print_warning "This will run continuously. Press Ctrl+C to stop."
    docker-compose --profile sync run --rm mongosync /scripts/realtime-sync.sh
}

# 验证同步结果
verify_sync() {
    print_info "Verifying sync results..."
    docker-compose exec mongodb-source mongosh --username admin --password passwordA --authenticationDatabase admin --file /docker-entrypoint-initdb.d/verify-sync.js
}

# 清理环境
clean_environment() {
    print_warning "This will stop all containers and remove data volumes"
    read -p "Are you sure? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        print_info "Cleaning environment..."
        docker-compose down -v
        print_success "Environment cleaned"
    else
        print_info "Clean operation cancelled"
    fi
}

# 显示帮助信息
show_help() {
    echo "MongoDB Sync Runner"
    echo ""
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  full         Perform full sync (initial migration)"
    echo "  incremental  Perform incremental sync (append changes)"
    echo "  realtime     Perform real-time sync (continuous)"
    echo "  verify       Verify sync results"
    echo "  clean        Clean up environment (stop containers and remove volumes)"
    echo "  help         Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 full        # Start databases and perform full sync"
    echo "  $0 incremental # Perform incremental sync"
    echo "  $0 verify      # Verify sync results"
}

# 主函数
main() {
    local command=${1:-help}
    
    case $command in
        "full")
            check_dependencies
            start_databases
            full_sync
            ;;
        "incremental")
            check_dependencies
            incremental_sync
            ;;
        "realtime")
            check_dependencies
            start_databases
            realtime_sync
            ;;
        "verify")
            check_dependencies
            verify_sync
            ;;
        "clean")
            clean_environment
            ;;
        "help"|*)
            show_help
            ;;
    esac
}

# 运行主函数
main "$@" 